<template>
  <crec-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
    <span class="crec-pro-account-avatar">
      <crec-avatar size="small" :src="avatar" class="crec-pro-global-header-index-avatar" />
      <span>{{ currentUser.name }} </span>
      <crec-icon type="down" />
    </span>
    <template v-slot:overlay>
      <crec-menu class="crec-pro-drop-down menu" :selected-keys="[]">
        <crec-menu-divider v-if="menu" />
        <crec-menu-item key="logout" @click="handleLogout">
          <crec-icon type="logout" />
          登出
        </crec-menu-item>
      </crec-menu>
    </template>
  </crec-dropdown>
  <span v-else>
    <crec-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
import { Modal } from 'crec-ui'
import avatar from '@/assets/avatar2.jpg'
export default {
  name: 'AvatarDropdown',
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      avatar: avatar
    }
  },
  methods: {
    handleLogout (e) {
      Modal.confirm({
        title: this.$t('layouts.usermenu.dialog.content'),
        onOk: () => {
          return this.$store.dispatch('Logout')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.crec-pro-drop-down {
  /deep/ .crecion {
    margin-right: 8px;
  }
  /deep/ .crec-dropdown-menu-item {
    min-width: 100px;
  }
}
.icon {
  color: inherit !important;
}
</style>
